package com.ruoyi.admin.service.impl;

import java.util.List;

import com.ruoyi.admin.domain.dto.LabRequestDto;
import com.ruoyi.admin.domain.vo.LabRequestVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.LabrequestMapper;
import com.ruoyi.admin.domain.Labrequest;
import com.ruoyi.admin.service.ILabrequestService;

/**
 * 实验室申请Service业务层处理
 * 
 * @author lin
 * @date 2024-04-20
 */
@Service
@Slf4j
public class LabrequestServiceImpl implements ILabrequestService 
{
    @Autowired
    private LabrequestMapper labrequestMapper;

    /**
     * 查询实验室申请
     * 
     * @param requestId 实验室申请主键
     * @return 实验室申请
     */
    @Override
    public LabRequestVo selectLabrequestByRequestId(Long requestId)
    {
        return labrequestMapper.selectLabrequestByRequestId(requestId);
    }

    /**
     * 查询实验室申请列表
     * 
     * @param labrequest 实验室申请
     * @return 实验室申请
     */
    @Override
    public List<LabRequestVo> selectLabrequestList(LabRequestDto labrequest)
    {
        //数据隔离，管理员可以看到全部的数据，非管理员只能看到自己的数据
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId=user.getUserId();
        boolean isAdmin = user.getRoles().stream().anyMatch(item-> item.isAdmin());
        if(!isAdmin) {
            labrequest.setUserId(userId);
        }
        return labrequestMapper.selectLabrequestList(labrequest);
    }

    /**
     * 新增实验室申请
     * 
     * @param labrequest 实验室申请
     * @return 结果
     */
    @Override
    public int insertLabrequest(Labrequest labrequest)
    {
        return labrequestMapper.insertLabrequest(labrequest);
    }

    /**
     * 修改实验室申请
     * 
     * @param labrequest 实验室申请
     * @return 结果
     */
    @Override
    public int updateLabrequest(Labrequest labrequest)
    {

        String status = labrequest.getStatus();
        if ("未审核".equals(status)){
            status="0";
        }else if ("通过".equals(status)){
            status="1";
        }else if ("驳回".equals(status)){
            status="2";
        }else if ("使用完毕".equals(status)){
            status="3";
        }
        labrequest.setStatus(status);
        labrequest.setUpdateTime(DateUtils.getNowDate());
        return labrequestMapper.updateLabrequest(labrequest);
    }

    /**
     * 批量删除实验室申请
     * 
     * @param requestIds 需要删除的实验室申请主键
     * @return 结果
     */
    @Override
    public int deleteLabrequestByRequestIds(Long[] requestIds)
    {
        return labrequestMapper.deleteLabrequestByRequestIds(requestIds);
    }

    /**
     * 删除实验室申请信息
     * 
     * @param requestId 实验室申请主键
     * @return 结果
     */
    @Override
    public int deleteLabrequestByRequestId(Long requestId)
    {
        return labrequestMapper.deleteLabrequestByRequestId(requestId);
    }
}
